package dk.dashboardFP.panels.loggerPanel {
	import dk.dashboardFP.dfpLogger.view.LogItemViewEvent;
	import dk.dashboardFP.panels.subPanels.ui.windowSubPanelUI.scrollWindow.WindowContentScrollRequest;
	import dk.dashboardFP.dfpLogger.DFPLogItem;
	import dk.dashboardFP.dfpLogger.view.LogItemView;
	import dk.dashboardFP.panels.subPanels.ui.windowSubPanelUI.IWindowContent;
	import dk.dashboardFP.panels.subPanels.ui.windowSubPanelUI.WindowContentEvent;

	import flash.display.Sprite;
	import flash.geom.Point;

	/**
	 * @author josef
	 */
	public class LoggerWindowContent extends Sprite implements IWindowContent {

		private var _intCountY : Number = 0;
		private var _nWindowWidth : Number;
		private var _nWindowHeight : Number;
		private var _a_oDFPLogItems : Array = [];
		private var _nItemSpace : Number = 1;
		private var _o_doLogFilterItems : Object;
		private var _markedForHeightUpdate : Boolean=false;

		public function LoggerWindowContent(o_doLogFilterItems : Object) {
			_o_doLogFilterItems = o_doLogFilterItems;
			setupElements();
		}

		private function setupElements() : void {
			addEventListener(LogItemViewEvent.HEIGHT_UPDATED, onItemHeight, false, 0, true);
		}

		private function onItemHeight(event : LogItemViewEvent) : void {
			_markedForHeightUpdate=true;
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// update
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		public function updateFilters() : void {
			
			var bMarkForUpdate : Boolean = false;
			var doLogFilterItem : LogFilterItem;

			for each (var oDFPLogItem : DFPLogItem in _a_oDFPLogItems) {
				
				doLogFilterItem = _o_doLogFilterItems[oDFPLogItem.intGroup];
				
				
				if (!oDFPLogItem.bVisible && doLogFilterItem.oLogGroup.level <= oDFPLogItem.intLevel && doLogFilterItem.oCB.bValue) {
					bMarkForUpdate=true;
					showItem(oDFPLogItem);
				} else if (oDFPLogItem.bVisible && ((doLogFilterItem.oLogGroup.level > oDFPLogItem.intLevel || !doLogFilterItem.oCB.bValue))) {
					bMarkForUpdate=true;
					hideItem(oDFPLogItem);
				} 
				
				/*
				if (doLogFilterItem.oCB.bValue != oDFPLogItem.bVisible || (doLogFilterItem.oLogGroup.level > oDFPLogItem.intLevel && oDFPLogItem.bVisible)) {
					
					bMarkForUpdate = true;
					
					if (!doLogFilterItem.oCB.bValue || doLogFilterItem.oLogGroup.level > oDFPLogItem.intLevel) {
						hideItem(oDFPLogItem);
					} else {
						showItem(oDFPLogItem);
					}
				}
				 * 
				 */
				
				if (bMarkForUpdate) {
					updateItemPositionsY();
				}
			}
		}

		private function updateItemPositionsY() : void {
			_intCountY = 0;
			for each (var oDFPLogItem : DFPLogItem in _a_oDFPLogItems) {
				if (oDFPLogItem.bVisible) {
					oDFPLogItem.doView.y = _intCountY;
					_intCountY += oDFPLogItem.doView.height + _nItemSpace;
				} else if (oDFPLogItem.doView.y != 0) {
					oDFPLogItem.doView.y=0;
				}
			}
			
			_markedForHeightUpdate=false;
			
			dispatchHeightChange();
		}

		private function showItem(oDFPLogItem : DFPLogItem) : void {
			oDFPLogItem.doView.visible = true;
			oDFPLogItem.bVisible = true;
		}

		private function hideItem(oDFPLogItem : DFPLogItem) : void {
			oDFPLogItem.bVisible = false;
			oDFPLogItem.doView.visible = false;
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// add log
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		public function addLog(oDFPLogItem : DFPLogItem) : void {
			
			_a_oDFPLogItems[_a_oDFPLogItems.length] = oDFPLogItem;
			
			doAddItemView(oDFPLogItem);
			dispatchHeightChange();
		}

		private function doAddItemView(oDFPLogItem : DFPLogItem) : void {
			
			//:::::::
			// check view 
			if (!oDFPLogItem.doView) {
				createViewForItem(oDFPLogItem);
			}
			
			//:::::::
			//  hide show
			var doLogFilterItem:LogFilterItem = _o_doLogFilterItems[oDFPLogItem.intGroup];
			if (!doLogFilterItem.oCB.bValue || doLogFilterItem.oLogGroup.level > oDFPLogItem.intLevel) {
				hideItem(oDFPLogItem); 
			}
			
			//:::::::
			// add to display 
			addChild(oDFPLogItem.doView);
			
			if (oDFPLogItem.bVisible) {
				oDFPLogItem.doView.y = _intCountY;
				_intCountY += oDFPLogItem.doView.height + _nItemSpace;
			}
			
			//:::::::
			// update
			dispatchHeightChange(WindowContentScrollRequest.TO_BOTTOM);
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// view factory
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function createViewForItem(oDFPLogItem : DFPLogItem) : void {
			oDFPLogItem.doView = get_doLogItemView(oDFPLogItem);
			updateItemView(oDFPLogItem.doView);
		}

		private function get_doLogItemView(oDFPLogItem:DFPLogItem) : Sprite {
			
			if (oDFPLogItem.oData!=null) {
				return new DataLogItemView(oDFPLogItem);
			} else {
				return new LogItemView(oDFPLogItem);
			}
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// view update
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function updateItemView(doView : Sprite) : void {
			doView.width = _nWindowWidth;
		}

		private function updateWindowDimentions() : void {
			for each (var oDFPLogItem : DFPLogItem in _a_oDFPLogItems) {
				oDFPLogItem.doView.width = _nWindowWidth;
			}
			
			if (_markedForHeightUpdate) {
				updateItemPositionsY();
			}
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// height chnage
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function dispatchHeightChange(sScrollRequest:String=null) : void {
			dispatchEvent(new WindowContentEvent(WindowContentEvent.CONTENT_DIMENTIONS_CHANGE, sScrollRequest));
		}

		public function set nWindowWidth(nWindowWidth : Number) : void {
			_nWindowWidth = nWindowWidth;
		}

		public function set nWindowHeight(nWindowHeight : Number) : void {
			_nWindowHeight = nWindowHeight;
		}

		public function setWindowDimentions(pWindowDimentions : Point) : void {
			
			_nWindowWidth = pWindowDimentions.x;
			_nWindowHeight = pWindowDimentions.y;

			dispatchEvent(new WindowContentEvent(WindowContentEvent.CONTENT_DIMENTIONS_CHANGE));
			
			updateWindowDimentions();
		}
	}
}
